// assets/test/apply-corner.ts
import { _decorator, Component, ImageAsset, Sprite, Texture2D, UITransform, Vec2 } from 'cc';
import { patternConfigs } from './pattern-config';
import { createSpriteFrameFromPixelData, generatePixelData, VariationIntensity } from './imageGenerator';
import RenderContentData from './renderContentData';
const { ccclass, executeInEditMode, property } = _decorator;

@ccclass('ApplyCorner')
@executeInEditMode
export class ApplyCorner extends Component {
  @property(Texture2D)
  logo: Texture2D = null!;


  onLoad() {


    this.logo.setFilters(Texture2D.Filter.NEAREST, Texture2D.Filter.NEAREST)
    this.logo.setWrapMode(Texture2D.WrapMode.CLAMP_TO_BORDER, Texture2D.WrapMode.CLAMP_TO_BORDER)
    RenderContentData.I.init(this.node.getComponent(UITransform).width, this.node.getComponent(UITransform).height);
    let a = this.createTexture2D()
    // let texture = this.createTexture2D();
    const texture = this.createTexture2D2(this.node.getComponent(UITransform).width, this.node.getComponent(UITransform).height);
    const sprite = this.getComponent(Sprite)!;
    const mat = sprite.customMaterial || sprite.sharedMaterial;
    mat.setProperty('spriteSize', new Vec2(this.node.getComponent(UITransform).width, this.node.getComponent(UITransform).height));
    mat.setProperty('textureX', a);
    console.log(mat, 'mat')
    sprite.customMaterial = mat;
  }

  generatePixelData2(patternType = 'test',enableColorVariation=true,variationIntensity: VariationIntensity = "strong"){
    const config = patternConfigs[patternType];
    const { pixelData, width, height } = generatePixelData(config, enableColorVariation, variationIntensity);
    return {
      pixelData,
      width,
      height,
    }
  }
  createTexture2D2(width: number, height: number): Texture2D {
    // const data1 = this.generatePixelData2('L',true,'strong');
    const data1 = this.generatePixelData2('HEART',true,'strong');
    // const pixelData = this.concatUint8(data1.pixelData, data2.pixelData);
    // console.log(pixelData,'pixelData')
    const {spriteFrame,texture} = createSpriteFrameFromPixelData(data1.pixelData, data1.width, data1.height);
    return texture;
  }
  createTexture2D(): Texture2D {
    const { pixelData, width, height } = this.generatePixelData2();
    const {spriteFrame,texture} = createSpriteFrameFromPixelData(pixelData, width, height);
    return texture;
  }
  concatUint8(a: Uint8Array, b: Uint8Array): Uint8Array {
    const out = new Uint8Array(a.length + b.length);
    out.set(a, 0);
    out.set(b, a.length);
    return out;
  }
  overlayUint8(a: Uint8Array, b: Uint8Array): Uint8Array {
    const len = Math.min(a.length, b.length);
    const out = new Uint8Array(Math.max(a.length, b.length));
    out.set(a, 0);
    for (let i = 0; i < len; i += 4) {
      const ar = a[i], ag = a[i + 1], ab = a[i + 2], aa = a[i + 3];
      const br = b[i], bg = b[i + 1], bb = b[i + 2], ba = b[i + 3];
      if (ba > 0) {
        out[i] = br;
        out[i + 1] = bg;
        out[i + 2] = bb;
        out[i + 3] = ba;
      } else {
        out[i] = ar;
        out[i + 1] = ag;
        out[i + 2] = ab;
        out[i + 3] = aa;
      }
    }
    return out;
  }
  
}